<?php
/*
 * @灯商云端
 * @功能说明：用户中心
 * @更新说明：暂无更新
 * @文件名 UserController.class.php
 * @编码 UTF-8
 * @创建时间 2015-7-21 下午3:38:52
 * @创建人 XING
 */

namespace dg\Controller;
use dg\Model\ResEntityModel;

use Think\Controller;
use service\excel\PhpExcel;

class UserController extends ApibaseController {
	/*
	 * 登陆模块
	 * @date: 2015-3-21下午2:10:37
	 * @editor: XING
	 */
	public function login(){
		global $isLogin;
		if($isLogin)
			redirect('/goods/index/vid/1?res=0-2');
		else
    		$this->display('user/login');
    }
  
    /*
     * 系统用户注册
     * @date: 2015-3-21下午2:20:22
     * @editor: XING
     */
    public function regist(){
    	/*用户注册逻辑*/
    	if(I('post.username')){
    		$salts = randstr(5);
    		$data = array(
    				'username' => I('post.username'),
    				'password' => md5(I('post.passwd').$salts),
    				'salt' => $salts,
    				'email' => I('post.email'),
    				'remember_token' => '',
    				'add_at' => time(),
    				'update_at' => time()
    			);
    		if(M('ResEntity')->add($data)){
    			$this->showtips('添加经销商成功');
    		}else{
    			$this->showtips('操作失败');
    		}	
    	}

    	//注册页面渲染
    	$this->display('user/regist');
    }
    
    /*
     * 系统登陆
     * @date: 2015-3-21下午2:48:06
     * @editor: XING
     */
    public function checklogin(){
    	$userMsg = M('ResEntity')->where(array('username'=>I('post.username')))->find();
    	if($userMsg['password'] == md5(I('post.passwd').$userMsg['salt'])){
    		$pushSession = array('name'=>I('post.username'));
    		if(I('post.signlogin') == 1){
    			$setSecret = md5(I('post.username').'ajwUid'.time());
    			M('ResEntity')->where(array('username'=>I('post.username')))->setField(
    				array('remember_token' => $setSecret)
    			);
    			cookie('_ajwunion',$setSecret,'expire=604800');
    			$pushSession['expire'] = 604800;
    		}
    		session($pushSession);
    		session('loginUid',$userMsg['rid']);
    		session('loginName',$userMsg['username']);
    		cookie('ajwUid',base64_encode(I('post.username')).randstr(),'expire=604800');
    		session(I('post.username'),true);
    		putlog('登陆系统','UserController','checklogin');
    		jsonRespons('1','登陆成功！');
    	}else{
    		jsonRespons('0','帐号或密码有误！');
    	}
    }
    
    /*
	 * 分销商城_临时租用
	 * @date: 2016-4-8 下午5:11:18
	 * @editor: XING
	 */
    public function printMsg(){
    	if(isset($_GET) && $_GET){
    		session_destroy();exit;
    		file_put_contents('123.txt', json_encode($_GET));
    	}else{
	    	file_put_contents('123.txt', 'nodata');
    	}
    }
    
    /*
     * 系统注销操作
     * @date: 2015-3-23下午12:02:30
     * @editor: XING
     */
    public function loginout(){
    	$userMsg = getLoginMsg();
    	session(cookie('PHPSESSID'),null);
    	session(getLoginMsg()->username,null);
    	cookie('ajwUid',null);
    	cookie('_ajwunion',null);
    	cookie('PHPSESSID',null);

    	redirect('/user/login');
    }
    
    /*
     * 个人中心简介
     * @date: 2015-3-23下午2:30:20
     * @editor: XING
     */
    public function usercenter(){
    	
    	//个人页面渲染
    	$this->display('user/usercenter');
    }
    
    /*
     * 个人中心资料编辑
     * @date: 2015-3-23下午5:06:13
     * @editor: XING
     */
    public function userUpdate(){
    	if(I('post.update')){
    		$data = array('email'=>I('post.email'));
    		if(I('post.password')){
    			$salt = randstr(5);
    			$data['password'] = md5(I('post.password').$salt);
    			$data['salt'] = $salt;
    		}
    		$data['email'] = I('post.email');
    		session('kfLine'.I('post.rid'),I('post.lineStatus') == '1' ? '1' : null);
    		$keArr = session('kfLineArr');
    		if(I('post.lineStatus') == '1'){
    			if(isset($keArr[I('post.rid')]) && $keArr[I('post.rid')] = I('post.rid')){
    				// Nothing to do
    			}else{
    				$keArr[I('post.rid')] = I('post.rid');
    			}
    			session('kfLineArr',$keArr);
    		}else{
    			if(isset($keArr[I('post.rid')])){
    				unset($keArr[I('post.rid')]);
    				session('kfLineArr',$keArr);
    			}
    		}
    		M('ResEntity')->where(array('rid'=>session('loginUid')))->setField($data);
    		
    		$this->showtips('信息编辑成功');
    	}
    }
}